<template lang="pug">
.supplier
  .supplier-header
    .supplier-header-search
      el-input(placeholder="供应商名称/联系人" prefix-icon="el-icon-search" v-model="searchString" clearable @change="changeSearch")
    .supplier-header-info 设置品质供应商，最多设置5个供应商
  .supplier-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
      :cell-style="cellStyle"
    )
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column(label="供应商名称" prop="companyName" min-width="100")
        template(slot-scope="scope")
          .table-item-bd(@click="seeDetail(scope.row)")
            .table-item-case-name {{scope.row.companyName}}
      el-table-column(label="联系人" prop="personsInChargeOfCompany")
      el-table-column(label="联系电话" prop="personsInChargeOfCompanyPhone")
      el-table-column(label="创建时间" sortable prop="createDate")
        template(slot-scope="scope") {{scope.row.createDate | filterDate}}
      el-table-column(label="上架材料数量" sortable min-width="100" align="right" prop="materialAmount")
      el-table-column(label="案例数量" sortable align="right" prop="caseAmount")
      el-table-column(label="操作" v-if="$permission('AB-01005-000')")
        template(slot-scope="scope")
          .table-item-handle
            el-switch(
              v-model="scope.row.isQuality"
              active-color="#8676FF"
              inactive-color="#B2B4C7"
              :disabled="scope.row.isDisable"
              @change="changeClassCase(scope.row)"
            )
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
  el-dialog(
    :visible.sync="showDialog"
    top="5vh"
    width="560px"
    @open="beforeOpen"
    :before-close="handleClose"
    :close-on-click-modal="false"
  )
    .modal-title(slot="title") 查看供应商用户
    .modal-content
      .form-item.start
        .form-item-label 公司logo：
        .form-item-value
          .form-item-logo(:style="detail.companyLogoUrl | filterUrl")
      .form-item
        .form-item-label 供应商名称：
        .form-item-value {{detail.companyName ? detail.companyName : '/'}}
      .form-item
        .form-item-label 登录账户：
        .form-item-value {{detail.userName ? detail.userName : '/'}}
      .form-item
        .form-item-label 供应商状态：
        .form-item-value 正常
      .form-item
        .form-item-label 手机号：
        .form-item-value {{detail.headOfSalesPhone ? detail.headOfSalesPhone : '/'}}
      .form-item
        .form-item-label 公司类型：
        .form-item-value {{detail.companyType | filterSupplier}}
      .form-item
        .form-item-label 自营业态：
        .form-item-value.flex
          .tag-item(v-for="item in detail.styleList") {{item.styleName}}
      .form-item
        .form-item-label 主营产品：
        .form-item-value
          .tag-item(v-for="item in detail.supplierProductList") {{item.classificationName}}
      .form-item
        .form-item-label 负责人：
        .form-item-value {{detail.personsInChargeOfCompany ? detail.personsInChargeOfCompany : '/'}}
      .form-item
        .form-item-label 负责人手机：
        .form-item-value {{detail.personsInChargeOfCompanyPhone ? detail.personsInChargeOfCompanyPhone : '/'}}
      .form-item
        .form-item-label 销售负责人：
        .form-item-value {{detail.headOfSales ? detail.headOfSales : '/'}}
      .form-item
        .form-item-label 销售负责人手机：
        .form-item-value {{detail.headOfSalesPhone ? detail.headOfSalesPhone : '/'}}
      .form-item
        .form-item-label 联系地址：
        .form-item-value {{detail.detailAddress ? detail.detailAddress : '/'}}
    .modal-footer(slot="footer")
      el-button(type="primary" @click="handleClose") 确定
</template>

<script>
import { getIndexuppliersList, setQuality, getSupplierDetail } from 'api'
export default {
  name: 'supplier',
  data () {
    return {
      loading: false,
      total: 0,
      page: 1,
      pageSize: 20,
      searchString: '',
      list: [],
      showDialog: false,
      currentItem: {},
      detail: {}
    }
  },
  computed: {
    maxNum () {
      let len = this.list.filter(item => item.isQuality).length
      if (len >= 5) {
        this.list.forEach(item => {
          if (!item.isQuality) item.isDisable = true
          else item.isDisable = false
        })
      } else {
        this.list.forEach(item => {
          item.isDisable = false
        })
      }
      return len
    }
  },
  methods: {
    async changeClassCase (item) {
      try {
        if (this.maxNum > 5) {
          this.$message.error('经典案例最多设置5个！')
          return
        }
        await this.$axios.post(setQuality, { supplierId: item.id, isQuality: item.isQuality })
        this.$message.success('设置成功！')
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    cellStyle ({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0 && row.isQuality) {
        return { background: '#FFF5F8' }
      }
    },
    currentChange (page) {
      this.page = page
      this.getList()
    },
    changeSearch () {
      this.page = 1
      this.getList()
    },
    async beforeOpen () {
      try {
        const { ResultSet } = await this.$axios.post(getSupplierDetail, { userId: this.currentItem.userId })
        this.detail = ResultSet.Result[0]
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    handleClose () {
      this.currentItem = {}
      this.showDialog = false
    },
    seeDetail (item) {
      this.currentItem = item
      this.showDialog = true
    },
    async getList () {
      try {
        let query = { ...this.$route.query, currentPage: this.page, pageLines: this.pageSize }
        if (this.searchString) query.searchString = this.searchString
        const { ResultSet } = await this.$axios.post(getIndexuppliersList, { ...query })
        let len = ResultSet.userData.supplierList.filter(item => item.isQuality).length
        if (len >= 5) {
          ResultSet.userData.supplierList.forEach(item => {
            if (!item.isQuality) item.isDisable = true
            else item.isDisable = false
          })
        }
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.supplierList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.getList()
  }
} 
</script>

<style type="text/css" lang="scss">
.supplier {
  .el-dialog__header {
    border-bottom: 1px solid #EDEDED !important;
  }
}
</style>

<style lang="scss" scoped>
.supplier {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.supplier-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
}
.supplier-header-info {
  font-size: 14px;
  line-height: 22px;
  color: #8B8EAB;
}
.supplier-table {
  height: calc(100% - 150px);
}
.table-item-case-name {
  color: #FF2E42;
}
.table-item-header {
  display: flex;
  align-items: center;
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.modal-title {
  font-weight: bold;
  font-size: 16px;
  line-height: 24px;
  color: #3E4373;
}
.table-item-bd {
  cursor: pointer;
}
.modal-content {
  .form-item {
    &:last-child {
      margin-bottom: 0;
    }
  }
}
.form-item {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  &.start {
    align-items: flex-start;
    .form-item-label {
      line-height: 30px;
    }
  }
  .form-item-label {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
    min-width: 150px;
    text-align: right;
  }
  .form-item-value {
    flex: 1;
    font-size: 14px;
    line-height: 22px;
    margin-left: 8px;
    color: #3E4373;
    &.flex {
      display: flex;
      align-items: center;
    }
    .tag-item {
      display: inline-block;
      padding: 1px 10px;
      background: #F4F1FF;
      border-radius: 6px;
      margin-right: 8px;
      font-size: 14px;
      line-height: 22px;
    }
  }
  .form-item-logo {
    width: 80px;
    height: 80px;
    border-radius: 6px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    background-color: #F6F6F9;
  }
}
</style>
